home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / os2 / e33el2.zip / emacs / 19.33 / lisp / f90.el < prev    next >
Lisp/Scheme  |  1996-05-23  |  71KB  |  1,645 lines

  1. ;;; f90.el --- Fortran-90 mode (free format)
  2.  
  3. ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Torbj\"orn Einarsson <T.Einarsson@clab.ericsson.se>
  6. ;; Created: Apr. 18, 1996
  7. ;; Keywords: fortran, f90, languages
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; Smart mode for editing F90 programs in FREE FORMAT.
  29. ;; Knows about continuation lines, named structured statements, and other
  30. ;; new features in F90 including HPF (High Performance Fortran) structures.
  31. ;; The basic feature is to provide an accurate indentation of F90 programs.
  32. ;; In addition, there are many more features like automatic matching of all
  33. ;; end statements, an auto-fill function to break long lines, a join-lines
  34. ;; function which joins continued lines etc etc.
  35. ;;  To facilitate typing, a fairly complete list of abbreviations is provided.
  36. ;;    For example, `i is short-hand for integer (if abbrev-mode is on).
  37.  
  38. ;; There are two separate features for highlighting the code.
  39. ;;   1) Upcasing or capitalizing of all keywords.
  40. ;;   2) Colors/fonts using font-lock-mode. (only when using X-windows)
  41. ;;  Automatic upcase of downcase of keywords is controlled by the parameter
  42. ;;  f90-auto-keyword-case.
  43.  
  44. ;; The indentations of lines starting with ! is determined by the first of the
  45. ;; following matches (the values in the left column are the default values):
  46.  
  47. ;; start-string/regexp  indent         variable holding start-string/regexp
  48. ;;    !!!                  0
  49. ;;    !hpf\\$ (re)         0              f90-directive-comment-re
  50. ;;    !!$                  0              f90-comment-region
  51. ;;    !      (re)        as code          f90-indented-comment-re
  52. ;;    default            comment-column
  53.  
  54. ;; Ex: Here is the result of 3 different settings of f90-indented-comment-re
  55. ;;     f90-indented-comment-re  !-indentation      !!-indentation
  56. ;;          !                    as code             as code
  57. ;;          !!                   comment-column      as code
  58. ;;          ![^!]                as code             comment-column
  59. ;; Trailing comments are indented to comment-column with indent-for-comment M-;
  60. ;; f90-comment-region (C-c;) toggles insertion of f90-comment-region in region.
  61.  
  62. ;; One common convention for free vs. fixed format is that free-format files
  63. ;; have the ending .f90 while the fixed format files have the ending .f. 
  64. ;; To make f90-mode work, put this file in, for example, your directory
  65. ;;  ~/lisp, and be sure that you have the following in your .emacs-file
  66. ;;     (setq load-path (append load-path '("~/lisp")))
  67. ;;     (autoload 'f90-mode "f90"
  68. ;;       "Major mode for editing Fortran 90 code in free format." t)
  69. ;;     (setq auto-mode-alist (append auto-mode-alist 
  70. ;;                           (list '("\\.f90$" . f90-mode))))
  71. ;; Once you have entered f90-mode, you may get more info by using
  72. ;; the command describe-mode (C-h m). For online help describing various
  73. ;; functions use  C-h f <Name of function you want described>
  74.  
  75. ;; To customize the f90-mode for your taste, use, for example:
  76. ;;    (you don't have to specify values for all the parameters below)
  77. ;;(setq f90-mode-hook
  78. ;;      '(lambda () (setq f90-do-indent 3
  79. ;;                        f90-if-indent 3
  80. ;;                        f90-type-indent 3
  81. ;;                        f90-program-indent 2
  82. ;;                        f90-continuation-indent 5
  83. ;;                        f90-comment-region "!!$"
  84. ;;                        f90-directive-comment-re "!hpf\\$"
  85. ;;                        f90-indented-comment-re "!"
  86. ;;                        f90-break-delimiters "[-+\\*/,><=% \t]"
  87. ;;                        f90-break-before-delimiters t
  88. ;;                        f90-beginning-ampersand t
  89. ;;                        f90-smart-end 'blink
  90. ;;                        f90-auto-keyword-case nil
  91. ;;                        f90-leave-line-no  nil
  92. ;;                        f90-startup-message t
  93. ;;                        indent-tabs-mode nil
  94. ;;                  )
  95. ;;       ;;The rest is not default.
  96. ;;       (abbrev-mode 1)             ; turn on abbreviation mode
  97. ;;       (f90-auto-fill-mode 1)      ; turn on auto-filling
  98. ;;       (turn-on-font-lock)         ; for highlighting
  99. ;;       (if f90-auto-keyword-case   ; change case of all keywords on startup
  100. ;;           (f90-change-keywords f90-auto-keyword-case))
  101. ;;     ))
  102. ;; in your .emacs file (the shown values are the defaults). You can also
  103. ;; change the values of the lists f90-keywords etc.
  104. ;; The auto-fill and abbreviation minor modes are accessible from the menu,
  105. ;; or by using M-x f90-auto-fill-mode and M-x abbrev-mode, respectively.
  106.  
  107. ;; Remarks
  108. ;; 1) Line numbers are by default left-justified. If f90-leave-line-no is
  109. ;;    non-nil, the line numbers are never touched.
  110. ;; 2) Multi-; statements like > do i=1,20 ; j=j+i ; end do < are not handled
  111. ;;    correctly, but I imagine them to be rare.
  112. ;; 3) Regexps for hilit19 are no longer supported.
  113. ;; 4) For FIXED FORMAT code, use the ordinary fortran mode.
  114. ;; 5) This mode does not work under emacs-18.x.
  115. ;; 6) Preprocessor directives, i.e., lines starting with # are left-justified
  116. ;;    and are untouched by all case-changing commands. There is, at present, no
  117. ;;    mechanism for treating multi-line directives (continued by \ ).
  118. ;; 7) f77 do-loops do 10 i=.. ; ; 10 continue are not correctly indented.
  119. ;;    You are urged to use f90-do loops (with labels if you wish).
  120. ;; 8) The highlighting mode under XEmacs is not as complete as under Emacs.
  121.  
  122. ;; List of user commands
  123. ;;   f90-previous-statement         f90-next-statement
  124. ;;   f90-beginning-of-subprogram    f90-end-of-subprogram   f90-mark-subprogram
  125. ;;   f90-comment-region
  126. ;;   f90-indent-line                f90-indent-new-line
  127. ;;   f90-indent-region    (can be called by calling indent-region)
  128. ;;   f90-indent-subprogram
  129. ;;   f90-break-line                 f90-join-lines
  130. ;;   f90-auto-fill-mode
  131. ;;   f90-fill-region
  132. ;;   f90-insert-end
  133. ;;   f90-upcase-keywords            f90-upcase-region-keywords
  134. ;;   f90-downcase-keywords          f90-downcase-region-keywords
  135. ;;   f90-capitalize-keywords        f90-capitalize-region-keywords
  136.  
  137. ;; Thanks to all the people who have tested the mode. Special thanks to Jens
  138. ;; Bloch Helmers for encouraging me to write this code, for creative
  139. ;; suggestions as well as for the lists of hpf-commands.
  140. ;; Also thanks to the authors of the fortran and pascal modes, on which some
  141. ;; of this code is built.
  142.  
  143. ;;; Code:
  144.  
  145. (defconst bug-f90-mode "T.Einarsson@clab.ericsson.se"
  146.   "Address of mailing list for F90 mode bugs.")
  147.  
  148. ;; User options
  149. (defvar f90-do-indent 3
  150.   "*Extra indentation applied to DO blocks.")
  151.  
  152. (defvar f90-if-indent 3
  153.   "*Extra indentation applied to IF, SELECT CASE, WHERE and FORALL blocks.")
  154.  
  155. (defvar f90-type-indent 3
  156.   "*Extra indentation applied to TYPE, INTERFACE and BLOCK DATA blocks.")
  157.  
  158. (defvar f90-program-indent 2
  159.   "*Extra indentation applied to PROGRAM/MODULE/SUBROUTINE/FUNCTION blocks.")
  160.  
  161. (defvar f90-continuation-indent 5
  162.   "*Extra indentation applied to F90 continuation lines.")
  163.  
  164. (defvar f90-comment-region "!!$"
  165.   "*String inserted by \\[f90-comment-region]\
  166.  at start of each line in region.")
  167.  
  168. (defvar f90-indented-comment-re "!"
  169.   "*Regexp saying which comments to be indented like code.")
  170.  
  171. (defvar f90-directive-comment-re "!hpf\\$"
  172.   "*Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented.")
  173.  
  174. (defvar f90-beginning-ampersand t
  175.   "*t makes automatic insertion of \& at beginning of continuation line.")
  176.  
  177. (defvar f90-smart-end 'blink
  178.   "*From an END statement, check and fill the end using matching block start.
  179. Allowed values are 'blink, 'no-blink, and nil, which determine
  180. whether to blink the matching beginning.")
  181.  
  182. (defvar f90-break-delimiters "[-+\\*/><=,% \t]"
  183.   "*Regexp holding list of delimiters at which lines may be broken.")
  184.  
  185. (defvar f90-break-before-delimiters t
  186.   "*Non-nil causes `f90-do-auto-fill' to break lines before delimiters.")
  187.  
  188. (defvar f90-auto-keyword-case nil
  189.   "*Automatic case conversion of keywords.
  190.   The options are 'downcase-word, 'upcase-word, 'capitalize-word and nil")
  191.  
  192. (defvar f90-leave-line-no nil
  193.   "*If nil, left-justify linenumbers.")
  194.  
  195. (defvar f90-startup-message t
  196.   "*Non-nil displays a startup message when F90 mode is first called.")
  197.  
  198. (defconst f90-keywords-re
  199.   ;;("allocate" "allocatable" "assign" "assignment" "backspace" "block"
  200.   ;;"call" "case" "character" "close" "common" "complex" "contains"
  201.   ;;"continue" "cycle" "data" "deallocate" "dimension" "do" "double" "else"
  202.   ;;"elseif" "elsewhere" "end" "enddo" "endfile" "endif" "entry" "equivalence"
  203.   ;;"exit" "external" "forall" "format" "function" "goto" "if" "implicit"
  204.   ;;"include" "inquire" "integer" "intent" "interface" "intrinsic" "logical"
  205.   ;;"module" "namelist" "none" "nullify" "only" "open" "operator" "optional" "parameter"
  206.   ;;"pause" "pointer" "precision" "print" "private" "procedure" "program"
  207.   ;;"public" "read" "real" "recursive" "result" "return" "rewind" "save" "select"
  208.   ;;"sequence" "stop" "subroutine" "target" "then" "type" "use" "where"
  209.   ;;"while" "write")
  210.   (concat
  211.    "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|b\\(ackspace\\|"
  212.    "lock\\)\\|c\\(a\\(ll\\|se\\)\\|haracter\\|lose\\|o\\(m\\(mon\\|plex\\)\\|"
  213.    "nt\\(ains\\|inue\\)\\)\\|ycle\\)\\|d\\(ata\\|eallocate\\|imension\\|"
  214.    "o\\(\\|uble\\)\\)\\|e\\(lse\\(\\|if\\|where\\)\\|n\\(d\\(\\|do\\|file\\|"
  215.    "if\\)\\|try\\)\\|quivalence\\|x\\(it\\|ternal\\)\\)\\|f\\(or\\(all\\|"
  216.    "mat\\)\\|unction\\)\\|goto\\|i\\(f\\|mplicit\\|n\\(clude\\|quire\\|t\\("
  217.    "e\\(ger\\|nt\\|rface\\)\\|rinsic\\)\\)\\)\\|logical\\|module\\|n\\("
  218.    "amelist\\|one\\|ullify\\)\\|o\\(nly\\|p\\(en\\|erator\\|tional\\)\\)\\|p\\(a\\("
  219.    "rameter\\|use\\)\\|ointer\\|r\\(ecision\\|i\\(nt\\|vate\\)\\|o\\("
  220.    "cedure\\|gram\\)\\)\\|ublic\\)\\|re\\(a[dl]\\|cursive\\|sult\\|turn\\|wind\\)\\|"
  221.    "s\\(ave\\|e\\(lect\\|quence\\)\\|top\\|ubroutine\\)\\|t\\(arget\\|hen\\|"
  222.    "ype\\)\\|use\\|w\\(h\\(ere\\|ile\\)\\|rite\\)\\)\\>")
  223.   "Regexp for F90 keywords.")
  224.  
  225. (defconst f90-keywords-level-3-re
  226.   ;; ("allocate" "allocatable" "assign" "assignment" "backspace" "close"
  227.   ;; "deallocate" "dimension" "endfile" "entry" "equivalence" "external"
  228.   ;; "inquire" "intent" "intrinsic" "nullify" "only" "open" "operator"
  229.   ;; "optional" "parameter" "pause" "pointer" "print" "private" "public"
  230.   ;; "read" "recursive" "result" "rewind" "save" "select" "sequence"
  231.   ;; "target"  "write")
  232.   (concat
  233.    "\\<\\(a\\(llocat\\(able\\|e\\)\\|ssign\\(\\|ment\\)\\)\\|backspace\\|"
  234.    "close\\|d\\(eallocate\\|imension\\)\\|e\\(n\\(dfile\\|try\\)\\|"
  235.    "quivalence\\|xternal\\)\\|"
  236.    "in\\(quire\\|t\\(ent\\|rinsic\\)\\)\\|nullify\\|"
  237.    "o\\(nly\\|p\\(en\\|erator\\|tional\\)\\)\\|"
  238.    "p\\(a\\(rameter\\|use\\)\\|ointer\\|ri\\(nt\\|vate\\)\\|ublic\\)\\|re\\("
  239.    "ad\\|cursive\\|sult\\|wind\\)\\|s\\(ave\\|e\\(lect\\|quence\\)\\)\\|target\\|"
  240.    "write\\)\\>")
  241. "Keyword-regexp for font-lock level >= 3.")
  242.  
  243.  
  244. (defconst f90-procedures-re
  245.   ;; ("abs" "achar" "acos" "adjustl" "adjustr" "aimag" "aint" "all" "allocated"
  246.   ;; "anint" "any" "asin" "associated" "atan" "atan2" "bit_size" "btest"
  247.   ;; "ceiling" "char" "cmplx" "conjg" "cos" "cosh" "count" "cshift"
  248.   ;; "date_and_time" "dble" "digits" "dim" "dot_product" "dprod" "eoshift"
  249.   ;; "epsilon" "exp" "exponent" "floor" "fraction" "huge" "iachar" "iand"
  250.   ;; "ibclr" "ibits" "ibset" "ichar" "ieor" "index" "int" "ior" "ishft"
  251.   ;; "ishftc" "kind" "lbound" "len" "len_trim" "lge" "lgt" "lle" "llt" "log"
  252.   ;; "logical" "log10" "matmul" "max" "maxexponent" "maxloc" "maxval" "merge"
  253.   ;; "min" "minexponent" "minloc" "minval" "mod" "modulo" "mvbits" "nearest"
  254.   ;; "nint" "not" "pack" "precision" "present" "product" "radix"
  255.   ;; "random_number" "random_seed" "range" "real" "repeat" "reshape"
  256.   ;; "rrspacing" "scale" "scan" "selected_int_kind" "selected_real_kind"
  257.   ;; "set_exponent" "shape" "sign" "sin" "sinh" "size" "spacing" "spread"
  258.   ;; "sqrt" "sum" "system_clock" "tan" "tanh" "tiny" "transfer" "transpose"
  259.   ;; "trim" "ubound" "unpack" "verify")
  260.   ;; A left parenthesis to avoid highlighting non-procedures.
  261.   ;; Real is taken out here to avoid highlighting declarations.
  262.   (concat
  263.    "\\<\\(a\\(bs\\|c\\(har\\|os\\)\\|djust[lr]\\|i\\(mag\\|nt\\)\\|ll\\(\\|"
  264.    "ocated\\)\\|n\\(int\\|y\\)\\|s\\(in\\|sociated\\)\\|tan2?\\)\\|b\\("
  265.    "it_size\\|test\\)\\|c\\(eiling\\|har\\|mplx\\|o\\(njg\\|sh?\\|unt\\)\\|"
  266.    "shift\\)\\|d\\(ate_and_time\\|ble\\|i\\(gits\\|m\\)\\|ot_product\\|prod"
  267.    "\\)\\|e\\(oshift\\|psilon\\|xp\\(\\|onent\\)\\)\\|f\\(loor\\|"
  268.    "raction\\)\\|huge\\|i\\(a\\(char\\|nd\\)\\|b\\(clr\\|its\\|set\\)\\|"
  269.    "char\\|eor\\|n\\(dex\\|t\\)\\|or\\|shftc?\\)\\|kind\\|l\\(bound\\|"
  270.    "en\\(\\|_trim\\)\\|g[et]\\|l[et]\\|og\\(\\|10\\|ical\\)\\)\\|m\\(a\\("
  271.    "tmul\\|x\\(\\|exponent\\|loc\\|val\\)\\)\\|erge\\|in\\(\\|exponent\\|"
  272.    "loc\\|val\\)\\|od\\(\\|ulo\\)\\|vbits\\)\\|n\\(earest\\|int\\|ot\\)\\|"
  273.    "p\\(ack\\|r\\(e\\(cision\\|sent\\)\\|oduct\\)\\)\\|r\\(a\\(dix\\|n\\("
  274.    "dom_\\(number\\|seed\\)\\|ge\\)\\)\\|e\\(peat\\|shape\\)\\|rspacing\\)\\|"
  275.    "s\\(ca\\(le\\|n\\)\\|e\\(lected_\\(int_kind\\|real_kind\\)\\|"
  276.    "t_exponent\\)\\|hape\\|i\\(gn\\|nh?\\|ze\\)\\|p\\(acing\\|read\\)\\|"
  277.    "qrt\\|um\\|ystem_clock\\)\\|t\\(anh?\\|iny\\|r\\(ans\\(fer\\|pose\\)\\|"
  278.    "im\\)\\)\\|u\\(bound\\|npack\\)\\|verify\\)[ \t]*(")
  279.   "Regexp whose first part matches F90 intrinsic procedures.")
  280.  
  281. (defconst f90-operators-re
  282. ;; "and" "or" "not" "eqv" "neqv" "eq" "ne" "lt" "le" "gt" "ge" "true" "false"
  283.  (concat
  284.    "\\.\\(and\\|eqv?\\|false\\|g[et]\\|l[et]\\|n\\(e\\(\\|qv\\)\\|"
  285.    "ot\\)\\|or\\|true\\)\\.")
  286.   "Regexp matching intrinsic operators.")
  287.  
  288. (defconst f90-hpf-keywords-re
  289.   ;; Intrinsic procedures
  290.   ;; ("all_prefix" "all_scatter" "all_suffix" "any_prefix" "any_scatter"
  291.   ;; "any_suffix" "copy_prefix" "copy_scatter" "copy_suffix" "count_prefix"
  292.   ;; "count_scatter" "count_suffix" "grade_down" "grade_up" "hpf_alignment"
  293.   ;; "hpf_template" "hpf_distribution" "iall" "iall_prefix" "iall_scatter"
  294.   ;; "iall_suffix" "iany" "iany_prefix" "iany_scatter" "iany_suffix" "iparity"
  295.   ;; "iparity_prefix" "iparity_scatter" "iparity_suffix" "leadz"
  296.   ;; "maxval_prefix" "maxval_scatter" "maxval_suffix" "minval_prefix"
  297.   ;; "minval_scatter" "minval_suffix" "parity" "parity_prefix"
  298.   ;; "parity_scatter" "parity_suffix" "popcnt" "poppar" "product_prefix"
  299.   ;; "product_scatter" "product_suffix" "sum_prefix" "sum_scatter"
  300.   ;; "sum_suffix" "ilen" "number_of_processors" "processors_shape")
  301.   ;; Directives
  302.   ;; ("align" "distribute" "dynamic" "inherit" "template" "processors"
  303.   ;; "realign" "redistribute" "independent")
  304.   ;; Keywords
  305.   ;; ("pure" "extrinsic" "new" "with" "onto" "block" "cyclic")
  306.   (concat
  307.    "\\<\\(a\\(l\\(ign\\|l_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny_\\("
  308.    "prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|block\\|c\\(o\\(py_\\(prefix\\|"
  309.    "s\\(catter\\|uffix\\)\\)\\|unt_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
  310.    "yclic\\)\\|d\\(istribute\\|ynamic\\)\\|extrinsic\\|grade_\\(down\\|"
  311.    "up\\)\\|hpf_\\(alignment\\|distribution\\|template\\)\\|i\\(a\\(ll\\(\\|"
  312.    "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|ny\\(\\|_\\(prefix\\|s\\("
  313.    "catter\\|uffix\\)\\)\\)\\)\\|len\\|n\\(dependent\\|herit\\)\\|parity\\(\\|"
  314.    "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\)\\|leadz\\|m\\(axval_\\("
  315.    "prefix\\|s\\(catter\\|uffix\\)\\)\\|inval_\\(prefix\\|s\\(catter\\|"
  316.    "uffix\\)\\)\\)\\|n\\(ew\\|umber_of_processors\\)\\|onto\\|p\\(arity\\(\\|"
  317.    "_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|op\\(cnt\\|par\\)\\|ro\\("
  318.    "cessors\\(\\|_shape\\)\\|duct_\\(prefix\\|s\\(catter\\|uffix\\)\\)\\)\\|"
  319.    "ure\\)\\|re\\(align\\|distribute\\)\\|sum_\\(prefix\\|s\\(catter\\|"
  320.    "uffix\\)\\)\\|template\\|with\\)\\>")
  321.   "Regexp for all HPF keywords, procedures and directives.")
  322.  
  323. ;; Highlighting patterns
  324.  
  325. (defvar f90-font-lock-keywords-1
  326.   (if (string-match "XEmacs" emacs-version)
  327.       (list                ; XEmacs
  328.        '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>"
  329.      1 font-lock-keyword-face)
  330.        '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)"
  331.      3 font-lock-function-name-face)
  332.        '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>"
  333.      1 font-lock-keyword-face)
  334.        '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)"
  335.      2 font-lock-function-name-face nil t)
  336.        ;; Special highlighting of "module procedure foo-list"
  337.        '("\\<\\(module[ \t]*procedure\\)\\>" 1 font-lock-keyword-face t)
  338.        ;; Highlight definition of new type
  339.        '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
  340.      1 font-lock-keyword-face)
  341.        '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
  342.      3 font-lock-function-name-face)
  343.        "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>")
  344.     (list                ; Emacs
  345.      '("\\<\\(end[ \t]*\\(program\\|module\\|function\\|subroutine\\|type\\)\\)\\>[ \t]*\\(\\sw+\\)?"
  346.        (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
  347.      '("\\<\\(program\\|call\\|module\\|subroutine\\|function\\|use\\)\\>[ \t]*\\(\\sw+\\)?"
  348.        (1 font-lock-keyword-face) (2 font-lock-function-name-face nil t))
  349.      ;; Special highlighting of "module procedure foo-list"
  350.      '("\\<\\(module[ \t]*procedure\\)\\>" (1 font-lock-keyword-face t))
  351.      ;; Highlight definition of new type
  352.      '("\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)"
  353.        (1 font-lock-keyword-face) (3 font-lock-function-name-face))
  354.      "\\<\\(\\(end[ \t]*\\)?\\(interface\\|block[ \t]*data\\)\\|contains\\)\\>"))
  355.   "This does fairly subdued highlighting of comments and function calls.")
  356.  
  357. (defvar f90-font-lock-keywords-2
  358.   (append f90-font-lock-keywords-1
  359.      (if (string-match "XEmacs" emacs-version)
  360.       (list                ; XEmacs
  361.        ;; Variable declarations (avoid the real function call)
  362.        '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)"
  363.      1 font-lock-type-face)
  364.        '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
  365.      4 font-lock-doc-string-face)
  366.        ;; do, if and select constructs
  367.        '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>"
  368.      1 font-lock-keyword-face)
  369.        '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)"
  370.      3 font-lock-doc-string-face)
  371.        '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
  372.      2 font-lock-doc-string-face)
  373.        '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
  374.      3 font-lock-keyword-face)
  375.       ;; implicit declaration
  376.        '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
  377.      1 font-lock-keyword-face)
  378.        '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>"
  379.      2 font-lock-type-face)
  380.        '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/"
  381.      1 font-lock-keyword-face)
  382.        '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)\/"
  383.      2 font-lock-doc-string-face nil t)
  384.        '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
  385.        "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
  386.        "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
  387.        '("\\<\\(exit\\|cycle\\)\\>" 
  388.      1 font-lock-keyword-face)
  389.        '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)2\\>" 
  390.      2 font-lock-doc-string-face)
  391.        '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
  392.        '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
  393.      1 font-lock-keyword-face)
  394.        '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
  395.      2 font-lock-doc-string-face)
  396.        '("^[ \t]*\\([0-9]+\\)" 1 font-lock-doc-string-face t))
  397.       (list                ; Emacs
  398.        ;; Variable declarations (avoid the real function call)
  399.        '("^[ \t0-9]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\)\\(.*::\\|[ \t]*(.*)\\)?\\(.*\\)"
  400.      (1 font-lock-type-face) (4 font-lock-variable-name-face))
  401.        ;; do, if and select constructs
  402.        '("\\<\\(end[ \t]*\\(do\\|if\\|select\\)\\)\\>\\([ \t]+\\(\\sw+\\)\\)?"
  403.      (1 font-lock-keyword-face) (3 font-lock-reference-face nil t))
  404.        '("^[ \t0-9]*\\(\\(\\sw+\\)[ \t]*:[ \t]*\\)?\\(\\(if\\|do\\([ \t]*while\\)?\\|select[ \t]*case\\)\\)\\>"
  405.      (2 font-lock-reference-face nil t) (3 font-lock-keyword-face))
  406.        ;; implicit declaration
  407.        '("\\<\\(implicit\\)[ \t]*\\(real\\|integer\\|c\\(haracter\\|omplex\\)\\|logical\\|type[ \t]*(\\sw+)\\|none\\)\\>" (1 font-lock-keyword-face) (2 font-lock-type-face))
  408.        '("\\<\\(namelist\\|common\\)[ \t]*\/\\(\\sw+\\)?\/" (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
  409.        '("\\<\\(where\\|forall\\)[ \t]*(" . 1)
  410.        "\\<e\\(lse\\([ \t]*if\\|where\\)?\\|nd[ \t]*\\(where\\|forall\\)\\)\\>"
  411.        "\\<\\(then\\|continue\\|format\\|include\\|stop\\|return\\)\\>"
  412.        '("\\<\\(exit\\|cycle\\)[ \t]*\\(\\sw+\\)?\\>" 
  413.      (1 font-lock-keyword-face) (2 font-lock-reference-face nil t))
  414.        '("\\<\\(case\\)[ \t]*\\(default\\|(\\)" . 1)
  415.        '("\\<\\(do\\|go *to\\)\\>[ \t]*\\([0-9]+\\)"
  416.      (1 font-lock-keyword-face) (2 font-lock-reference-face))
  417.        '("^[ \t]*\\([0-9]+\\)" (1 font-lock-reference-face t)))))
  418.   "Highlights declarations, do-loops and other constructions")
  419.  
  420. (defvar f90-font-lock-keywords-3
  421.   (append f90-font-lock-keywords-2
  422.    (list
  423.     f90-keywords-level-3-re
  424.     f90-operators-re
  425.     (if (string-match "XEmacs" emacs-version)
  426.     (append (list f90-procedures-re) '(1 font-lock-keyword-face t))
  427.       (list f90-procedures-re '(1 font-lock-keyword-face t)))
  428.     "\\<real\\>"            ; Avoid overwriting real defs.
  429.     ))
  430.   "Highlights all F90 keywords and intrinsic procedures.")
  431.  
  432. (defvar f90-font-lock-keywords-4
  433.   (append f90-font-lock-keywords-3
  434.     (list f90-hpf-keywords-re))
  435.   "Highlights all F90 and HPF keywords.")
  436.  
  437. (defvar f90-font-lock-keywords
  438.       f90-font-lock-keywords-2
  439.   "*Default expressions to highlight in F90 mode.")
  440.  
  441. ;; syntax table
  442. (defvar f90-mode-syntax-table nil
  443.   "Syntax table in use in F90 mode buffers.")
  444.  
  445. (if f90-mode-syntax-table
  446.     ()
  447.   (setq f90-mode-syntax-table (make-syntax-table))
  448.   (modify-syntax-entry ?\! "<" f90-mode-syntax-table)  ; beg. comment
  449.   (modify-syntax-entry ?\n ">" f90-mode-syntax-table)  ; end comment
  450.   (modify-syntax-entry ?_ "w" f90-mode-syntax-table)   ; underscore in names
  451.   (modify-syntax-entry ?\' "\"" f90-mode-syntax-table) ; string quote
  452.   (modify-syntax-entry ?\" "\"" f90-mode-syntax-table) ; string quote
  453.   (modify-syntax-entry ?\` "w" f90-mode-syntax-table)  ; for abbrevs
  454.   (modify-syntax-entry ?\r " " f90-mode-syntax-table)  ; return is whitespace
  455.   (modify-syntax-entry ?+ "." f90-mode-syntax-table)  
  456.   (modify-syntax-entry ?- "." f90-mode-syntax-table)
  457.   (modify-syntax-entry ?= "." f90-mode-syntax-table)
  458.   (modify-syntax-entry ?* "." f90-mode-syntax-table)
  459.   (modify-syntax-entry ?/ "." f90-mode-syntax-table)
  460.   (modify-syntax-entry ?\\ "/" f90-mode-syntax-table)) ; escape chars
  461.  
  462. ;; keys
  463. (defvar f90-mode-map ()
  464.   "Keymap used in F90 mode.")
  465.  
  466. (if f90-mode-map
  467.     ()
  468.   (setq f90-mode-map (make-sparse-keymap))
  469.   (define-key f90-mode-map "`"        'f90-abbrev-start)
  470.   (define-key f90-mode-map "\C-c;"    'f90-comment-region)
  471.   (define-key f90-mode-map "\C-\M-a"  'f90-beginning-of-subprogram)
  472.   (define-key f90-mode-map "\C-\M-e"  'f90-end-of-subprogram)
  473.   (define-key f90-mode-map "\C-\M-h"  'f90-mark-subprogram)
  474.   (define-key f90-mode-map "\C-\M-q"  'f90-indent-subprogram)
  475.   (define-key f90-mode-map "\C-j"     'f90-indent-new-line) ; LFD equals C-j
  476.   (define-key f90-mode-map "\r"       'newline)
  477.   (define-key f90-mode-map "\C-c\r"   'f90-break-line)
  478.   ;;  (define-key f90-mode-map [M-return] 'f90-break-line)
  479.   (define-key f90-mode-map "\C-c\C-d" 'f90-join-lines)
  480.   (define-key f90-mode-map "\C-c\C-f" 'f90-fill-region)
  481.   (define-key f90-mode-map "\C-c\C-p" 'f90-previous-statement)
  482.   (define-key f90-mode-map "\C-c\C-n" 'f90-next-statement)
  483.   (define-key f90-mode-map "\C-c\C-w" 'f90-insert-end)
  484.   (define-key f90-mode-map "\t"       'f90-indent-line))
  485.  
  486. ;; menus
  487. (if (string-match "XEmacs" emacs-version)
  488.     (defvar f90-xemacs-menu
  489.       '("F90"
  490.     ["Indent Subprogram"       f90-indent-subprogram t]
  491.     ["Mark Subprogram"         f90-mark-subprogram t]
  492.     ["Beginning of Subprogram" f90-beginning-of-subprogram t]
  493.     ["End of Subprogram"       f90-end-of-subprogram t]
  494.     "-----"
  495.     ["(Un)Comment Region"      f90-comment-region t]
  496.     ["Indent Region"           indent-region t]
  497.     ["Fill Region"             f90-fill-region t]
  498.     "-----"
  499.     ["Break Line at Point"     f90-break-line t]
  500.     ["Join with Next Line"     f90-join-lines t]
  501.     ["Insert Newline"          newline t]
  502.     ["Insert End"              f90-insert-end t]
  503.     "-----"
  504.     ["Upcase Keywords (buffer)"      f90-upcase-keywords t]
  505.     ["Upcase Keywords (region)"      f90-upcase-region-keywords
  506.      t]
  507.     ["Capitalize Keywords (buffer)"  f90-capitalize-keywords t]
  508.     ["Capitalize Keywords (region)" 
  509.      f90-capitalize-region-keywords t]
  510.     ["Downcase Keywords (buffer)"    f90-downcase-keywords t]
  511.     ["Downcase Keywords (region)"   
  512.      f90-downcase-region-keywords t]
  513.     "-----"
  514.     ["Toggle abbrev-mode"   abbrev-mode             t]
  515.     ["Toggle auto-fill"     f90-auto-fill-mode      t])
  516.       "XEmacs menu for F90 mode.")
  517.   ;; Emacs
  518.   (define-key f90-mode-map [menu-bar] (make-sparse-keymap))
  519.   (define-key f90-mode-map [menu-bar f90] 
  520.     (cons "F90" (make-sparse-keymap "f90"))) 
  521.   (define-key f90-mode-map [menu-bar f90 abbrev-mode]
  522.     '("Toggle abbrev-mode" . abbrev-mode))
  523.   (define-key f90-mode-map [menu-bar f90 f90-auto-fill-mode]
  524.     '("Toggle auto-fill" . f90-auto-fill-mode))
  525.   (define-key f90-mode-map [menu-bar f90 f90-downcase-region-keywords]
  526.     '("Downcase Keywords (region)" . f90-downcase-region-keywords))
  527.   (define-key f90-mode-map [menu-bar f90 f90-downcase-keywords]
  528.     '("Downcase Keywords (buffer)" . f90-downcase-keywords))
  529.   (define-key f90-mode-map [menu-bar f90 f90-capitalize-keywords]
  530.     '("Capitalize Keywords (region)" . f90-capitalize-region-keywords))
  531.   (define-key f90-mode-map [menu-bar f90 f90-capitalize-region-keywords]
  532.     '("Capitalize Keywords (buffer)" . f90-capitalize-keywords))
  533.   (define-key f90-mode-map [menu-bar f90 f90-upcase-region-keywords]
  534.     '("Upcase Keywords (region)" . f90-upcase-region-keywords))
  535.   (define-key f90-mode-map [menu-bar f90 f90-upcase-keywords]
  536.     '("Upcase Keywords (buffer)" . f90-upcase-keywords))
  537.   (define-key f90-mode-map [menu-bar f90 f90-insert-end]
  538.     '("Insert end" . f90-insert-end))
  539.   (define-key f90-mode-map [menu-bar f90 f90-join-lines]
  540.     '("Join with Next Line" . f90-join-lines))
  541.   (define-key f90-mode-map [menu-bar f90 f90-break-line]
  542.     '("Break Line at Point" . f90-break-line))
  543.   (define-key f90-mode-map [menu-bar f90 f90-fill-region]
  544.     '("Fill Region" . f90-fill-region))
  545.   (define-key f90-mode-map [menu-bar f90 indent-region]
  546.     '("Indent Region" . indent-region))
  547.   (define-key f90-mode-map [menu-bar f90 f90-comment-region]
  548.     '("(Un)Comment Region" . f90-comment-region))
  549.   (define-key f90-mode-map [menu-bar f90 f90-end-of-subprogram]
  550.     '("End of Subprogram" . f90-end-of-subprogram))
  551.   (define-key f90-mode-map [menu-bar f90 f90-beginning-of-subprogram]
  552.     '("Beginning of Subprogram" . f90-beginning-of-subprogram))
  553.   (define-key f90-mode-map [menu-bar f90 f90-mark-subprogram]
  554.     '("Mark Subprogram" . f90-mark-subprogram))
  555.   (define-key f90-mode-map [menu-bar f90 f90-indent-subprogram]
  556.     '("Indent Subprogram" . f90-indent-subprogram)))
  557.   
  558. ;; Regexps for finding program structures.
  559. (defconst f90-blocks-re 
  560.   "\\(block[ \t]*data\\|do\\|if\\|interface\\|function\\|module\\|\
  561. program\\|select\\|subroutine\\|type\\|where\\|forall\\)\\>")
  562. (defconst f90-program-block-re 
  563.   "\\(program\\|module\\|subroutine\\|function\\)")
  564. (defconst f90-else-like-re 
  565.   "\\(else\\([ \t]*if\\|where\\)?\\|case[ \t]*\\(default\\|(\\)\\)")
  566. (defconst f90-end-if-re 
  567.   "end[ \t]*\\(if\\|select\\|where\\|forall\\)\\>")
  568. (defconst f90-end-type-re 
  569.   "end[ \t]*\\(type\\|interface\\|block[ \t]*data\\)")
  570. (defconst f90-type-def-re
  571.   "\\<\\(type\\)[ \t]*\\(,.*::[ \t]*\\|[ \t]+\\)\\(\\sw+\\)")
  572. (defconst f90-no-break-re  "\\(\\*\\*\\|//\\|=>\\)")
  573. ;; A temporary position to make region operators faster
  574. (defvar f90-cache-position nil)
  575. (make-variable-buffer-local 'f90-cache-position)
  576.  
  577. ;; Imenu support
  578. (defvar f90-imenu-generic-expression
  579.   (cons
  580.    (concat
  581.     "^[ \t0-9]*\\("
  582.     "program[ \t]+\\(\\sw+\\)\\|"
  583.     "module[ \t]+\\(\\sw+\\)[ \t]*\\(!\\|$\\)\\|"
  584.     "\\(pure\\|recursive\\|extrinsic([^)]+)\\)?[ \t]*"
  585.     "subroutine[ \t]+\\(\\sw+\\)\\|"
  586.     ; avoid end function, but allow for most other things
  587.     "\\([^!]*\\([^e!].[^ \t!]\\|.[^n!][^ \t!]\\|..[^d \t!]\\)"
  588.     "\\|[^!]?[^!]?\\)[ \t]*function[ \t]+\\(\\sw+\\)"
  589.     "\\)")
  590.    (list 2 3 6 9))
  591.   "imenu generic expression for F90 mode.")
  592.  
  593. ;; When compiling under GNU Emacs, load imenu during compilation.  If
  594. ;; you have 19.22 or earlier, comment this out, or get imenu.
  595. (and (fboundp 'eval-when-compile)
  596.      (eval-when-compile
  597.        (if (not (string-match "XEmacs" emacs-version))
  598.        (require 'imenu))
  599.        ()))
  600.  
  601.  
  602.  
  603. ;; abbrevs have generally two letters, except standard types `c, `i, `r, `t
  604. (defvar f90-mode-abbrev-table nil)
  605. (if f90-mode-abbrev-table
  606.     ()
  607.   (let ((ac abbrevs-changed))
  608.     (define-abbrev-table 'f90-mode-abbrev-table ())
  609.     (define-abbrev f90-mode-abbrev-table  "`al"  "allocate" nil)
  610.     (define-abbrev f90-mode-abbrev-table  "`ab"  "allocatable" nil)
  611.     (define-abbrev f90-mode-abbrev-table  "`as"  "assignment" nil)
  612.     (define-abbrev f90-mode-abbrev-table  "`ba"  "backspace" nil)
  613.     (define-abbrev f90-mode-abbrev-table  "`bd"  "block data" nil)
  614.     (define-abbrev f90-mode-abbrev-table  "`c"   "character" nil)
  615.     (define-abbrev f90-mode-abbrev-table  "`cl"  "close" nil)
  616.     (define-abbrev f90-mode-abbrev-table  "`cm"  "common" nil)
  617.     (define-abbrev f90-mode-abbrev-table  "`cx"  "complex" nil)
  618.     (define-abbrev f90-mode-abbrev-table  "`cn"  "contains" nil)
  619.     (define-abbrev f90-mode-abbrev-table  "`cy"  "cycle" nil)
  620.     (define-abbrev f90-mode-abbrev-table  "`de"  "deallocate" nil)
  621.     (define-abbrev f90-mode-abbrev-table  "`df"  "define" nil)
  622.     (define-abbrev f90-mode-abbrev-table  "`di"  "dimension" nil)
  623.     (define-abbrev f90-mode-abbrev-table  "`dw"  "do while" nil)
  624.     (define-abbrev f90-mode-abbrev-table  "`el"  "else" nil)
  625.     (define-abbrev f90-mode-abbrev-table  "`eli" "else if" nil)
  626.     (define-abbrev f90-mode-abbrev-table  "`elw" "elsewhere" nil)
  627.     (define-abbrev f90-mode-abbrev-table  "`eq"  "equivalence" nil)
  628.     (define-abbrev f90-mode-abbrev-table  "`ex"  "external" nil)
  629.     (define-abbrev f90-mode-abbrev-table  "`ey"  "entry" nil)
  630.     (define-abbrev f90-mode-abbrev-table  "`fl"  "forall" nil)
  631.     (define-abbrev f90-mode-abbrev-table  "`fo"  "format" nil)
  632.     (define-abbrev f90-mode-abbrev-table  "`fu"  "function" nil)
  633.     (define-abbrev f90-mode-abbrev-table  "`fa"  ".false." nil)
  634.     (define-abbrev f90-mode-abbrev-table  "`im"  "implicit none" nil)
  635.     (define-abbrev f90-mode-abbrev-table  "`in " "include" nil)
  636.     (define-abbrev f90-mode-abbrev-table  "`i"   "integer" nil)
  637.     (define-abbrev f90-mode-abbrev-table  "`it"  "intent" nil)
  638.     (define-abbrev f90-mode-abbrev-table  "`if"  "interface" nil)
  639.     (define-abbrev f90-mode-abbrev-table  "`lo"  "logical" nil)
  640.     (define-abbrev f90-mode-abbrev-table  "`mo"  "module" nil)
  641.     (define-abbrev f90-mode-abbrev-table  "`na"  "namelist" nil)
  642.     (define-abbrev f90-mode-abbrev-table  "`nu"  "nullify" nil)
  643.     (define-abbrev f90-mode-abbrev-table  "`op"  "optional" nil)
  644.     (define-abbrev f90-mode-abbrev-table  "`pa"  "parameter" nil)
  645.     (define-abbrev f90-mode-abbrev-table  "`po"  "pointer" nil)
  646.     (define-abbrev f90-mode-abbrev-table  "`pr"  "print" nil)
  647.     (define-abbrev f90-mode-abbrev-table  "`pi"  "private" nil)
  648.     (define-abbrev f90-mode-abbrev-table  "`pm"  "program" nil)
  649.     (define-abbrev f90-mode-abbrev-table  "`pu"  "public" nil)
  650.     (define-abbrev f90-mode-abbrev-table  "`r"   "real" nil)
  651.     (define-abbrev f90-mode-abbrev-table  "`rc"  "recursive" nil)
  652.     (define-abbrev f90-mode-abbrev-table  "`rt"  "return" nil)
  653.     (define-abbrev f90-mode-abbrev-table  "`rw"  "rewind" nil)
  654.     (define-abbrev f90-mode-abbrev-table  "`se"  "select" nil)
  655.     (define-abbrev f90-mode-abbrev-table  "`sq"  "sequence" nil)
  656.     (define-abbrev f90-mode-abbrev-table  "`su"  "subroutine" nil)
  657.     (define-abbrev f90-mode-abbrev-table  "`ta"  "target" nil)
  658.     (define-abbrev f90-mode-abbrev-table  "`tr"  ".true." nil)
  659.     (define-abbrev f90-mode-abbrev-table  "`t"   "type" nil)
  660.     (define-abbrev f90-mode-abbrev-table  "`wh"  "where" nil)
  661.     (define-abbrev f90-mode-abbrev-table  "`wr"  "write" nil)
  662.     (setq abbrevs-changed ac)))
  663.  
  664. ;;;###autoload
  665. (defun f90-mode ()
  666.   "Major mode for editing Fortran 90 code in free format.
  667.  
  668. \\[f90-indent-new-line] corrects current indentation and creates new\
  669.  indented line.
  670. \\[f90-indent-line] indents the current line correctly. 
  671. \\[f90-indent-subprogram] indents the current subprogram. 
  672.  
  673. Type `? or `\\[help-command] to display a list of built-in\
  674.  abbrevs for F90 keywords.
  675.  
  676. Key definitions:
  677. \\{f90-mode-map}
  678.  
  679. Variables controlling indentation style and extra features:
  680.  
  681.  f90-do-indent
  682.     Extra indentation within do blocks.  (default 3)
  683.  f90-if-indent
  684.     Extra indentation within if/select case/where/forall blocks. (default 3)
  685.  f90-type-indent
  686.     Extra indentation within type/interface/block-data blocks.  (default 3)
  687.  f90-program-indent
  688.     Extra indentation within program/module/subroutine/function blocks.
  689.       (default 2)
  690.  f90-continuation-indent
  691.     Extra indentation applied to continuation lines.  (default 5)
  692.  f90-comment-region
  693.     String inserted by \\[f90-comment-region] at start of each line in 
  694.     region.  (default \"!!!$\")
  695.  f90-indented-comment-re
  696.     Regexp determining the type of comment to be intended like code.
  697.     (default \"!\")
  698.  f90-directive-comment-re
  699.     Regexp of comment-like directive like \"!HPF\\\\$\", not to be indented.
  700.     (default \"!hpf\\\\$\")
  701.  f90-break-delimiters
  702.     Regexp holding list of delimiters at which lines may be broken.
  703.     (default \"[-+*/><=,% \\t]\")
  704.  f90-break-before-delimiters
  705.     Non-nil causes `f90-do-auto-fill' to break lines before delimiters.
  706.     (default t)
  707.  f90-beginning-ampersand 
  708.     Automatic insertion of \& at beginning of continuation lines. (default t)
  709.  f90-smart-end 
  710.     From an END statement, check and fill the end using matching block start.
  711.     Allowed values are 'blink, 'no-blink, and nil, which determine
  712.     whether to blink the matching beginning.) (default 'blink)
  713.  f90-auto-keyword-case
  714.     Automatic change of case of keywords. (default nil)
  715.     The possibilities are 'downcase-word, 'upcase-word, 'capitalize-word.
  716.  f90-leave-line-no
  717.     Do not left-justify line numbers. (default nil)
  718.  f90-startup-message
  719.     Set to nil to inhibit message first time F90 mode is used. (default t)
  720.  f90-keywords-re
  721.     List of keywords used for highlighting/upcase-keywords etc.
  722.  
  723. Turning on F90 mode calls the value of the variable `f90-mode-hook'
  724. with no args, if that value is non-nil."
  725.   (interactive)
  726.   (kill-all-local-variables)
  727.   (setq major-mode 'f90-mode)
  728.   (setq mode-name "F90")
  729.   (setq local-abbrev-table f90-mode-abbrev-table)
  730.   (set-syntax-table f90-mode-syntax-table)
  731.   (use-local-map f90-mode-map)
  732.   (make-local-variable 'indent-line-function)
  733.   (setq indent-line-function 'f90-indent-line)
  734.   (make-local-variable 'indent-region-function)
  735.   (setq indent-region-function 'f90-indent-region)
  736.   (make-local-variable 'require-final-newline)
  737.   (setq require-final-newline t)
  738.   (make-local-variable 'comment-start)
  739.   (setq comment-start "!")
  740.   (make-local-variable 'comment-start-skip)
  741.   (setq comment-start-skip "!+ *")
  742.   (make-local-variable 'comment-indent-function)
  743.   (setq comment-indent-function 'f90-comment-indent)
  744.   (make-local-variable 'abbrev-all-caps)
  745.   (setq abbrev-all-caps t)
  746.   (setq indent-tabs-mode nil)
  747.   ;; Setting up things for font-lock
  748.   (if (string-match "Xemacs" emacs-version)
  749.       (progn
  750.     (put 'f90-mode 'font-lock-keywords-case-fold-search t)
  751.     (if (and current-menubar
  752.          (not (assoc "F90" current-menubar)))
  753.         (progn
  754.           (set-buffer-menubar (copy-sequence current-menubar))
  755.           (add-submenu nil f90-xemacs-menu)))
  756.     (make-local-variable 'font-lock-keywords)
  757.     (setq font-lock-keywords f90-font-lock-keywords))
  758.     ;; Emacs
  759.     (make-local-variable 'font-lock-defaults)
  760.     (setq font-lock-defaults 
  761.       '((f90-font-lock-keywords f90-font-lock-keywords-1
  762.                     f90-font-lock-keywords-2
  763.                     f90-font-lock-keywords-3
  764.                     f90-font-lock-keywords-4)
  765.         nil t))
  766.     ;; Tell imenu how to handle f90.
  767.     (make-local-variable 'imenu-generic-expression)
  768.     (setq imenu-generic-expression f90-imenu-generic-expression))
  769.   (run-hooks 'f90-mode-hook)
  770.   (if f90-startup-message
  771.       (message "Emacs F90 mode; please report bugs to %s" bug-f90-mode))
  772.   (setq f90-startup-message nil))
  773.  
  774. ;; inline-functions
  775. (defsubst f90-get-beg-of-line ()
  776.   (save-excursion (beginning-of-line) (point)))
  777.  
  778. (defsubst f90-get-end-of-line ()
  779.   (save-excursion (end-of-line) (point)))
  780.  
  781. (defsubst f90-in-string ()
  782.   (let ((beg-pnt
  783.      (if (and f90-cache-position (> (point) f90-cache-position))
  784.          f90-cache-position
  785.        (point-min))))
  786.     (nth 3 (parse-partial-sexp beg-pnt (point)))))
  787.         
  788. (defsubst f90-in-comment ()
  789.   (let ((beg-pnt
  790.      (if (and f90-cache-position (> (point) f90-cache-position))
  791.          f90-cache-position
  792.        (point-min))))
  793.     (nth 4 (parse-partial-sexp beg-pnt (point)))))
  794.  
  795. (defsubst f90-line-continued ()
  796.   (save-excursion
  797.     (let ((bol (f90-get-beg-of-line)))
  798.       (end-of-line)
  799.       (while (f90-in-comment)
  800.     (search-backward "!" bol)
  801.     (skip-chars-backward "!"))
  802.       (skip-chars-backward " \t")
  803.       (= (preceding-char) ?&))))
  804.  
  805. (defsubst f90-current-indentation ()
  806.   "Return indentation of current line.
  807. Line-numbers are considered whitespace characters."
  808.   (save-excursion
  809.     (beginning-of-line) (skip-chars-forward " \t0-9")
  810.     (current-column)))
  811.  
  812. (defsubst f90-indent-to (col &optional no-line-number)
  813.   "Indent current line to column COL.
  814. If no-line-number nil, jump over a possible line-number."
  815.   (beginning-of-line)
  816.   (if (not no-line-number)
  817.       (skip-chars-forward " \t0-9"))
  818.   (delete-horizontal-space)
  819.   (if (zerop (current-column))
  820.       (indent-to col)
  821.     (indent-to col 1)))
  822.  
  823. (defsubst f90-match-piece (arg)
  824.   (if (match-beginning arg)
  825.       (buffer-substring (match-beginning arg) (match-end arg))))
  826.  
  827. (defsubst f90-get-present-comment-type ()
  828.   (save-excursion
  829.     (let ((type nil) (eol (f90-get-end-of-line)))
  830.       (if (f90-in-comment)
  831.       (progn
  832.         (beginning-of-line)
  833.         (re-search-forward "[!]+" eol)
  834.         (while (f90-in-string)
  835.           (re-search-forward "[!]+" eol))
  836.         (setq type (buffer-substring (match-beginning 0) (match-end 0)))))
  837.       type)))
  838.  
  839. (defsubst f90-equal-symbols (a b)
  840.   "Compare strings neglecting case and allowing for nil value."
  841.   (let ((a-local (if a (downcase a) nil))
  842.     (b-local (if b (downcase b) nil)))
  843.     (equal a-local b-local)))
  844.  
  845. ;; XEmacs 19.11 & 19.12 gives back a single char when matching an empty regular
  846. ;; expression. Therefore, the next 2 functions are longer than necessary.
  847.  
  848. (defsubst f90-looking-at-do ()
  849.   "Return (\"do\" name) if a do statement starts after point.
  850. Name is nil if the statement has no label."
  851.   (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(do\\)\\>")
  852.       (let (label
  853.         (struct (f90-match-piece 3)))
  854.     (if (looking-at "\\(\\sw+\\)[ \t]*\:")
  855.         (setq label (f90-match-piece 1)))
  856.     (list struct label))))
  857.  
  858. (defsubst f90-looking-at-select-case ()
  859.   "Return (\"select\" name) if a select-case statement starts after point.
  860. Name is nil if the statement has no label."
  861.   (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(select\\)[ \t]*case[ \t]*(")
  862.       (let (label
  863.         (struct (f90-match-piece 3)))
  864.     (if (looking-at "\\(\\sw+\\)[ \t]*\:")
  865.         (setq label (f90-match-piece 1)))
  866.     (list struct label))))
  867.  
  868. (defsubst f90-looking-at-if-then ()
  869.   "Return (\"if\" name) if an if () then statement starts after point.
  870. Name is nil if the statement has no label."
  871.   (save-excursion
  872.     (let (struct (label nil))
  873.       (if (looking-at "\\(\\(\\sw+\\)[ \t]*\:\\)?[ \t]*\\(if\\)\\>")
  874.       (progn
  875.         (setq struct (f90-match-piece 3))
  876.         (if (looking-at "\\(\\sw+\\)[ \t]*\:")
  877.         (setq label (f90-match-piece 1)))
  878.         (goto-char (scan-lists (point) 1 0))
  879.         (skip-chars-forward " \t")
  880.         (if (or (looking-at "then\\>")
  881.             (if (f90-line-continued)
  882.             (progn
  883.               (f90-next-statement)
  884.               (skip-chars-forward " \t0-9&")
  885.               (looking-at "then\\>"))))
  886.         (list struct label)))))))
  887.  
  888. (defsubst f90-looking-at-where-or-forall ()
  889.   "Return (kind nil) if where/forall...end starts after point."
  890.   (save-excursion
  891.     (let (command)
  892.       (if (looking-at "\\(where\\|forall\\)[ \t]*(")
  893.       (progn
  894.         (setq command (list (f90-match-piece 1) nil))
  895.         (goto-char (scan-lists (point) 1 0))
  896.         (skip-chars-forward " \t")
  897.         (if (looking-at "\\(!\\|$\\)")
  898.         command))))))
  899.  
  900. (defsubst f90-looking-at-type-like ()
  901.   "Return (kind name) at the start of a type/interface/block-data block.
  902. Name is non-nil only for type."
  903.   (cond 
  904.    ((looking-at f90-type-def-re)
  905.     (list (f90-match-piece 1) (f90-match-piece 3)))
  906.    ((looking-at "\\(interface\\|block[\t]*data\\)\\>")
  907.     (list (f90-match-piece 1) nil))))
  908.  
  909. (defsubst f90-looking-at-program-block-start ()
  910.   "Return (kind name) if a program block with name name starts after point."
  911.   (cond
  912.    ((looking-at "\\(program\\)[ \t]+\\(\\sw+\\)\\>")
  913.     (list (f90-match-piece 1) (f90-match-piece 2)))
  914.    ((and (not (looking-at "module[ \t]*procedure\\>"))
  915.      (looking-at "\\(module\\)[ \t]+\\(\\sw+\\)\\>"))
  916.     (list (f90-match-piece 1) (f90-match-piece 2)))
  917.    ((looking-at (concat
  918.          "\\(pure\\|recursive\\|extrinsic([^)]+)\\)?[ \t]*"
  919.          "\\(subroutine\\)[ \t]+\\(\\sw+\\)"))
  920.     (list (f90-match-piece 2) (f90-match-piece 3)))
  921.    ((and (not (looking-at "end[ \t]*function"))
  922.      (looking-at "[^!\"\&\\n]*\\(function\\)[ \t]+\\(\\sw+\\)"))
  923.     (list (f90-match-piece 1) (f90-match-piece 2)))))
  924.  
  925. (defsubst f90-looking-at-program-block-end ()
  926.   "Return list of type and name of end of block."
  927.   (if (looking-at (concat "end[ \t]*" f90-blocks-re 
  928.               "?\\([ \t]+\\(\\sw+\\)\\)?\\>"))
  929.       (list (f90-match-piece 1) (f90-match-piece 3))))
  930.  
  931. (defsubst f90-comment-indent ()
  932.   (cond ((looking-at "!!!") 0)
  933.     ((and f90-directive-comment-re
  934.           (looking-at f90-directive-comment-re)) 0)
  935.     ((looking-at (regexp-quote f90-comment-region)) 0)
  936.     ((looking-at f90-indented-comment-re)
  937.      (f90-calculate-indent))
  938.     (t (skip-chars-backward " \t")
  939.        (max (if (bolp) 0 (1+ (current-column))) comment-column))))
  940.  
  941. (defsubst f90-present-statement-cont ()
  942.   "Return continuation properties of present statement."
  943.   (let (pcont cont)
  944.     (save-excursion
  945.       (setq pcont (if (f90-previous-statement) (f90-line-continued) nil)))
  946.     (setq cont (f90-line-continued))
  947.     (cond ((and (not pcont) (not cont)) 'single)
  948.        ((and (not pcont) cont)       'begin)
  949.        ((and pcont       (not cont)) 'end)
  950.        ((and pcont       cont)       'middle)
  951.        (t (error)))))
  952.  
  953. (defsubst f90-indent-line-no ()
  954.   (if f90-leave-line-no
  955.       ()
  956.     (if (and (not (zerop (skip-chars-forward " \t")))
  957.          (looking-at "[0-9]"))
  958.     (delete-horizontal-space)))
  959.   (skip-chars-forward " \t0-9"))
  960.  
  961. (defsubst f90-no-block-limit ()
  962.   (let ((eol (f90-get-end-of-line)))
  963.     (save-excursion
  964.       (not (or (looking-at "end")
  965.            (looking-at "\\(do\\|if\\|else\\|select[ \t]*case\\|\
  966. case\\|where\\|forall\\)\\>")
  967.            (looking-at "\\(program\\|module\\|interface\\|\
  968. block[ \t]*data\\)\\>")
  969.            (looking-at "\\(contains\\|\\sw+[ \t]*:\\)")
  970.            (looking-at f90-type-def-re)
  971.            (re-search-forward "\\(function\\|subroutine\\)" eol t))))))
  972.  
  973. (defsubst f90-update-line ()
  974.   (let (bol eol)
  975.     (if f90-auto-keyword-case
  976.     (progn (setq bol (f90-get-beg-of-line)
  977.              eol (f90-get-end-of-line))
  978.            (if f90-auto-keyword-case
  979.            (f90-change-keywords f90-auto-keyword-case bol eol))))))
  980.  
  981. (defun f90-get-correct-indent ()
  982.   "Get correct indent for a line starting with line number.
  983. Does not check type and subprogram indentation."
  984.   (let ((epnt (f90-get-end-of-line)) icol cont)
  985.     (save-excursion
  986.       (while (and (f90-previous-statement)
  987.           (or (progn
  988.             (setq cont (f90-present-statement-cont))
  989.             (or (eq cont 'end) (eq cont 'middle)))
  990.               (looking-at "[ \t]*[0-9]"))))
  991.       (setq icol (current-indentation))
  992.       (beginning-of-line)
  993.       (if (re-search-forward "\\(if\\|do\\|select\\|where\\|forall\\)"
  994.                  (f90-get-end-of-line) t)
  995.       (progn
  996.         (beginning-of-line) (skip-chars-forward " \t")
  997.         (cond ((f90-looking-at-do)
  998.            (setq icol (+ icol f90-do-indent)))
  999.           ((or (f90-looking-at-if-then)
  1000.                (f90-looking-at-where-or-forall)
  1001.                (f90-looking-at-select-case))
  1002.            (setq icol (+ icol f90-if-indent))))
  1003.         (end-of-line)))
  1004.       (while (re-search-forward
  1005.           "\\(if\\|do\\|select\\|where\\|forall\\)" epnt t)
  1006.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1007.     (cond  ((f90-looking-at-do)
  1008.         (setq icol (+ icol f90-do-indent)))
  1009.            ((or (f90-looking-at-if-then)
  1010.             (f90-looking-at-where-or-forall)
  1011.             (f90-looking-at-select-case))
  1012.         (setq icol (+ icol f90-if-indent)))
  1013.            ((looking-at f90-end-if-re)
  1014.         (setq icol (- icol f90-if-indent)))
  1015.            ((looking-at "end[ \t]*do\\>")
  1016.         (setq icol (- icol f90-do-indent))))
  1017.     (end-of-line))
  1018.       icol)))
  1019.            
  1020.       
  1021. (defun f90-calculate-indent ()
  1022.   "Calculate the indent column based on previous statements."
  1023.   (interactive)
  1024.   (let (icol cont (case-fold-search t) (pnt (point)))
  1025.     (save-excursion
  1026.       (if (not (f90-previous-statement))
  1027.       (setq icol 0)
  1028.     (setq cont (f90-present-statement-cont))
  1029.     (if (eq cont 'end)
  1030.         (while (not (eq 'begin (f90-present-statement-cont)))
  1031.           (f90-previous-statement)))
  1032.     (cond ((eq cont 'begin)
  1033.            (setq icol (+ (f90-current-indentation)
  1034.                  f90-continuation-indent)))
  1035.           ((eq cont 'middle) (setq icol(current-indentation)))
  1036.           (t (setq icol (f90-current-indentation))
  1037.          (skip-chars-forward " \t")
  1038.          (if (looking-at "[0-9]")
  1039.              (setq icol (f90-get-correct-indent))
  1040.            (cond ((or (f90-looking-at-if-then)
  1041.                   (f90-looking-at-where-or-forall)
  1042.                   (f90-looking-at-select-case)
  1043.                   (looking-at f90-else-like-re))               
  1044.               (setq icol (+ icol f90-if-indent)))
  1045.              ((f90-looking-at-do)
  1046.               (setq icol (+ icol f90-do-indent)))
  1047.              ((f90-looking-at-type-like)
  1048.               (setq icol (+ icol f90-type-indent)))
  1049.              ((or (f90-looking-at-program-block-start)
  1050.                   (looking-at "contains[ \t]*\\($\\|!\\)"))
  1051.               (setq icol (+ icol f90-program-indent)))))
  1052.          (goto-char pnt)
  1053.          (beginning-of-line)
  1054.          (cond ((looking-at "[ \t]*$"))
  1055.                ((looking-at "[ \t]*#") ; Check for cpp directive.
  1056.             (setq icol 0))
  1057.                (t
  1058.             (skip-chars-forward " \t0-9")
  1059.             (cond ((or (looking-at f90-else-like-re)
  1060.                    (looking-at f90-end-if-re))
  1061.                    (setq icol (- icol f90-if-indent)))
  1062.                   ((looking-at "end[ \t]*do\\>")
  1063.                    (setq icol (- icol f90-do-indent)))
  1064.                   ((looking-at f90-end-type-re)
  1065.                    (setq icol (- icol f90-type-indent)))
  1066.                   ((or (looking-at "contains[ \t]*\\(!\\|$\\)")
  1067.                    (f90-looking-at-program-block-end))
  1068.                    (setq icol (- icol f90-program-indent))))))
  1069.          ))))
  1070.     icol))
  1071.  
  1072. ;; Statement = statement line, a line which is neither blank, nor a comment.
  1073. (defun f90-previous-statement ()
  1074.   "Move point to beginning of the previous F90 statement.
  1075. Return nil if no previous statement is found."
  1076.   (interactive)
  1077.   (let (not-first-statement)
  1078.     (beginning-of-line)
  1079.     (while (and (setq not-first-statement (zerop (forward-line -1)))
  1080.         (looking-at "[ \t0-9]*\\(!\\|$\\|#\\)")))
  1081.     not-first-statement))
  1082.  
  1083. (defun f90-next-statement ()
  1084.   "Move point to beginning of the next F90 statement.
  1085. Return nil if no later statement is found."
  1086.   (interactive)
  1087.   (let (not-last-statement)
  1088.     (beginning-of-line)
  1089.     (while (and (setq not-last-statement
  1090.               (and (zerop (forward-line 1))
  1091.                (not (eobp))))
  1092.          (looking-at "[ \t0-9]*\\(!\\|$\\)")))
  1093.     not-last-statement))
  1094.  
  1095. (defun f90-beginning-of-subprogram ()
  1096.   "Move point to the beginning of subprogram.
  1097. Return (type name) or nil if not found."
  1098.   (interactive)
  1099.   (let ((count 1) (case-fold-search t) matching-beg)
  1100.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1101.     (if (setq matching-beg (f90-looking-at-program-block-start)) 
  1102.     (setq count (- count 1)))
  1103.     (while (and (not (zerop count))
  1104.         (re-search-backward f90-program-block-re nil 'move))
  1105.       (beginning-of-line) (skip-chars-forward " \t0-9")
  1106.       (cond 
  1107.        ((setq matching-beg (f90-looking-at-program-block-start))
  1108.     (setq count (- count 1)))
  1109.        ((f90-looking-at-program-block-end)
  1110.     (setq count (+ count 1)))))
  1111.     (beginning-of-line)
  1112.     (if (zerop count)
  1113.     matching-beg
  1114.       (message "No beginning-found.")
  1115.       nil)))
  1116.  
  1117. (defun f90-end-of-subprogram ()
  1118.   "Move point to the end of subprogram.
  1119. Return (type name) or nil if not found."
  1120.   (interactive)
  1121.   (let ((count 1) (case-fold-search t) matching-end)
  1122.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1123.     (if (setq matching-end (f90-looking-at-program-block-end))
  1124.     (setq count (1- count)))
  1125.     (end-of-line)
  1126.     (while (and (not (zerop count))
  1127.         (re-search-forward f90-program-block-re nil 'move))
  1128.       (beginning-of-line) (skip-chars-forward " \t0-9")
  1129.       (cond ((f90-looking-at-program-block-start)
  1130.          (setq count (+ count 1)))
  1131.         ((setq matching-end (f90-looking-at-program-block-end))
  1132.          (setq count (1- count ))))
  1133.       (end-of-line))
  1134.     (forward-line 1)
  1135.     (if (zerop count)
  1136.     matching-end
  1137.       (message "No end found.")
  1138.       nil)))
  1139.  
  1140. (defun f90-mark-subprogram ()
  1141.   "Put mark at end of F90 subprogram, point at beginning.
  1142. Marks are pushed and highlight (grey shadow) is turned on."
  1143.   (interactive)
  1144.   (let ((pos (point)) program)
  1145.     (f90-end-of-subprogram)
  1146.     (push-mark (point) t)
  1147.     (goto-char pos)
  1148.     (setq program (f90-beginning-of-subprogram))
  1149.     ;; The keywords in the preceding lists assume case-insensitivity.
  1150.     (if (string-match "XEmacs" emacs-version)
  1151.     (zmacs-activate-region)
  1152.       (setq mark-active t)
  1153.       (setq deactivate-mark nil))
  1154.     program))
  1155.  
  1156. (defun f90-comment-region (beg-region end-region)
  1157.   "Comment/uncomment every line in the region.
  1158. Insert f90-comment-region at the beginning of every line in the region
  1159. or, if already present, remove it."
  1160.   (interactive "*r")
  1161.   (let ((end (make-marker)))
  1162.     (set-marker end end-region)
  1163.     (goto-char beg-region)
  1164.     (beginning-of-line)
  1165.     (if (looking-at (regexp-quote f90-comment-region))
  1166.     (delete-region (point) (match-end 0))
  1167.       (insert f90-comment-region))
  1168.     (while (and  (zerop (forward-line 1))
  1169.          (< (point) (marker-position end)))
  1170.       (if (looking-at (regexp-quote f90-comment-region))
  1171.       (delete-region (point) (match-end 0))
  1172.     (insert f90-comment-region)))
  1173.     (set-marker end nil)))
  1174.  
  1175. (defun f90-indent-line (&optional no-update)
  1176.   "Indent current line as F90 code."
  1177.   (interactive)
  1178.   (let (indent (no-line-number nil) (pos (make-marker)) (case-fold-search t))
  1179.     (set-marker pos (point))
  1180.     (beginning-of-line)            ; Digits after & \n are not line-no
  1181.     (if (save-excursion (and (f90-previous-statement) (f90-line-continued)))
  1182.     (progn (setq no-line-number t) (skip-chars-forward " \t"))
  1183.       (f90-indent-line-no))
  1184.     (if (looking-at "!")
  1185.     (setq indent (f90-comment-indent))
  1186.       (if (and (looking-at "end") f90-smart-end)
  1187.         (f90-match-end))
  1188.       (setq indent (f90-calculate-indent)))
  1189.     (if (zerop (- indent (current-column)))
  1190.     nil
  1191.       (f90-indent-to indent no-line-number))
  1192.     ;; If initial point was within line's indentation,
  1193.     ;; position after the indentation.  Else stay at same point in text.
  1194.     (if (< (point) (marker-position pos))
  1195.     (goto-char (marker-position pos)))
  1196.     (if (not no-update) (f90-update-line))
  1197.     (if (and auto-fill-function
  1198.          (> (save-excursion (end-of-line) (current-column)) fill-column))
  1199.     (save-excursion (f90-do-auto-fill)))
  1200.     (set-marker pos nil)))
  1201.  
  1202. (defun f90-indent-new-line ()
  1203.   "Reindent the current F90 line, insert a newline and indent the newline.
  1204. An abbrev before point is expanded if `abbrev-mode' is non-nil.
  1205. If run in the middle of a line, the line is not broken."
  1206.   (interactive)
  1207.   (let (string cont (case-fold-search t))
  1208.     (if abbrev-mode (expand-abbrev))
  1209.     (beginning-of-line)            ; Reindent where likely to be needed.
  1210.     (f90-indent-line-no)
  1211.     (if (or (looking-at "\\(end\\|else\\|!\\)"))
  1212.     (f90-indent-line 'no-update))
  1213.     (end-of-line)
  1214.     (delete-horizontal-space)        ;Destroy trailing whitespace
  1215.     (setq string (f90-in-string))
  1216.     (setq cont (f90-line-continued))
  1217.     (if (and string (not cont)) (insert "&"))
  1218.     (f90-update-line)
  1219.     (newline)
  1220.     (if (or string (and cont f90-beginning-ampersand)) (insert "&"))
  1221.     (f90-indent-line 'no-update)))
  1222.  
  1223.  
  1224. (defun f90-indent-region (beg-region end-region)
  1225.   "Indent every line in region by forward parsing."
  1226.   (interactive "*r")
  1227.   (let ((end-region-mark (make-marker)) (save-point (point-marker))
  1228.     (block-list nil) ind-lev ind-curr ind-b cont
  1229.     struct beg-struct end-struct)
  1230.     (set-marker end-region-mark end-region)
  1231.     (goto-char beg-region)
  1232.     ;; first find a line which is not a continuation line or comment
  1233.     (beginning-of-line)
  1234.     (while (and (looking-at "[ \t]*[0-9]*\\(!\\|#\\|[ \t]*$\\)")
  1235.         (progn (f90-indent-line 'no-update)
  1236.                (zerop (forward-line 1)))
  1237.         (< (point) end-region-mark)))
  1238.     (setq cont (f90-present-statement-cont))
  1239.     (while (and (or (eq cont 'middle) (eq cont 'end))
  1240.         (f90-previous-statement))
  1241.       (setq cont (f90-present-statement-cont)))
  1242.     ;; process present line for beginning of block
  1243.     (setq f90-cache-position (point))
  1244.     (f90-indent-line 'no-update)
  1245.     (setq ind-lev (f90-current-indentation))
  1246.     (setq ind-curr ind-lev)
  1247.     (beginning-of-line) (skip-chars-forward " \t0-9")
  1248.     (setq struct nil)
  1249.     (setq ind-b (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1250.               ((or (setq struct (f90-looking-at-if-then))
  1251.                (setq struct (f90-looking-at-select-case))
  1252.                (setq struct (f90-looking-at-where-or-forall))
  1253.                (looking-at f90-else-like-re))
  1254.                f90-if-indent)
  1255.               ((setq struct (f90-looking-at-type-like))
  1256.                f90-type-indent)
  1257.               ((or(setq struct (f90-looking-at-program-block-start))
  1258.               (looking-at "contains[ \t]*\\($\\|!\\)"))
  1259.                f90-program-indent)))
  1260.     (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1261.     (if struct (setq block-list (cons struct block-list)))
  1262.     (while (and (f90-line-continued) (zerop (forward-line 1))
  1263.         (< (point) end-region-mark))
  1264.       (if (not (zerop (- (current-indentation) 
  1265.              (+ ind-curr f90-continuation-indent))))
  1266.       (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no)))
  1267.     ;; process all following lines
  1268.     (while (and  (zerop (forward-line 1)) (< (point) end-region-mark))
  1269.       (beginning-of-line)
  1270.       (f90-indent-line-no)
  1271.       (setq f90-cache-position (point))
  1272.       (cond ((looking-at "[ \t]*$") (setq ind-curr 0))
  1273.         ((looking-at "[ \t]*#") (setq ind-curr 0))
  1274.         ((looking-at "!") (setq ind-curr (f90-comment-indent)))
  1275.         ((f90-no-block-limit) (setq ind-curr ind-lev))
  1276.         ((looking-at f90-else-like-re) (setq ind-curr
  1277.                          (- ind-lev f90-if-indent)))
  1278.         ((looking-at "contains[ \t]*\\($\\|!\\)")
  1279.          (setq ind-curr (- ind-lev f90-program-indent)))
  1280.         ((setq ind-b
  1281.            (cond ((setq struct (f90-looking-at-do)) f90-do-indent)
  1282.              ((or (setq struct (f90-looking-at-if-then))
  1283.                   (setq struct (f90-looking-at-select-case))
  1284.                   (setq struct (f90-looking-at-where-or-forall)))
  1285.               f90-if-indent)
  1286.              ((setq struct (f90-looking-at-type-like))
  1287.               f90-type-indent)
  1288.              ((setq struct (f90-looking-at-program-block-start))
  1289.               f90-program-indent)))
  1290.          (setq ind-curr ind-lev)
  1291.          (if ind-b (setq ind-lev (+ ind-lev ind-b)))
  1292.          (setq block-list (cons struct block-list)))
  1293.         ((setq end-struct (f90-looking-at-program-block-end))
  1294.          (setq beg-struct (car block-list)
  1295.            block-list (cdr block-list))
  1296.          (if f90-smart-end 
  1297.          (save-excursion
  1298.            (f90-block-match (car beg-struct)(car (cdr beg-struct))
  1299.                     (car end-struct)(car (cdr end-struct)))))
  1300.          (setq ind-b
  1301.            (cond ((looking-at f90-end-if-re) f90-if-indent)
  1302.              ((looking-at "end[ \t]*do\\>")  f90-do-indent)
  1303.              ((looking-at f90-end-type-re) f90-type-indent)
  1304.              ((f90-looking-at-program-block-end)
  1305.               f90-program-indent)))
  1306.          (if ind-b (setq ind-lev (- ind-lev ind-b)))
  1307.          (setq ind-curr ind-lev))
  1308.         (t (setq ind-curr ind-lev)))
  1309.       ;; do the indentation if necessary
  1310.       (if (not (zerop (- ind-curr (current-column))))
  1311.       (f90-indent-to ind-curr))
  1312.       (while (and (f90-line-continued) (zerop (forward-line 1))
  1313.           (< (point) end-region-mark))
  1314.     (if (not (zerop (- (current-indentation) 
  1315.                (+ ind-curr f90-continuation-indent))))
  1316.         (f90-indent-to (+ ind-curr f90-continuation-indent) 'no-line-no))))
  1317.     ;; restore point etc
  1318.     (setq f90-cache-position nil)
  1319.     (goto-char save-point)
  1320.     (set-marker end-region-mark nil)
  1321.     (set-marker save-point nil)
  1322.     (if (string-match "Xemacs" emacs-version)
  1323.     (zmacs-deactivate-region)
  1324.       (deactivate-mark))))
  1325.  
  1326. (defun f90-indent-subprogram ()
  1327.   "Properly indent the subprogram which contains point."
  1328.   (interactive)
  1329.   (save-excursion
  1330.     (let (program)
  1331.       (setq program (f90-mark-subprogram))
  1332.       (if program
  1333.       (progn
  1334.         (message "Indenting %s %s..."
  1335.              (car program) (car (cdr program)))
  1336.         (f90-indent-region (point) (mark))
  1337.         (message "Indenting %s %s...done"
  1338.              (car program) (car (cdr program))))
  1339.     (message "Indenting the whole file...")
  1340.     (f90-indent-region (point) (mark))
  1341.     (message "Indenting the whole file...done")))))
  1342.  
  1343. ;; autofill and break-line
  1344. (defun f90-break-line (&optional no-update)
  1345.   "Break line at point, insert continuation marker(s) and indent."
  1346.   (interactive)
  1347.   (let (ctype)
  1348.     (cond ((f90-in-string)
  1349.        (insert "&") (newline) (insert "&"))
  1350.       ((f90-in-comment)
  1351.        (delete-horizontal-space)
  1352.        (setq ctype (f90-get-present-comment-type))
  1353.        (newline) (insert (concat ctype " ")))
  1354.       (t (delete-horizontal-space)
  1355.          (insert "&")
  1356.          (if (not no-update) (f90-update-line))
  1357.          (newline)
  1358.          (if f90-beginning-ampersand (insert "& ")))))
  1359.   (if (not no-update) (f90-indent-line)))
  1360.   
  1361. (defun f90-find-breakpoint ()
  1362.   "From fill-column, search backward for break-delimiter."
  1363.   (let ((bol (f90-get-beg-of-line)))
  1364.     (re-search-backward f90-break-delimiters bol)
  1365.     (if f90-break-before-delimiters
  1366.     (progn (backward-char)
  1367.            (if (not (looking-at f90-no-break-re))
  1368.            (forward-char)))
  1369.       (if (looking-at f90-no-break-re)
  1370.       (forward-char 2)
  1371.     (forward-char)))))
  1372.  
  1373. (defun f90-auto-fill-mode (arg)
  1374.   "Toggle f90-auto-fill mode.
  1375. With ARG, turn `f90-auto-fill' mode on iff ARG is positive.
  1376. In `f90-auto-fill' mode, inserting a space at a column beyond `fill-column'
  1377. automatically breaks the line at a previous space."
  1378.   (interactive "P")
  1379.   (prog1 (setq auto-fill-function
  1380.            (if (if (null arg)
  1381.                (not auto-fill-function)
  1382.              (> (prefix-numeric-value arg) 0))
  1383.            'f90-do-auto-fill))
  1384.     (force-mode-line-update)))
  1385.  
  1386. (defun f90-do-auto-fill ()
  1387.   "Break line if non-white characters beyond fill-column."
  1388.   (interactive)
  1389.   ;; Break the line before or after the last delimiter (non-word char).
  1390.   ;; Will not break **, //, or => (specified by f90-no-break-re).
  1391.   ;; Start by checking that line is longer than fill-column.
  1392.   (if (> (save-excursion (end-of-line) (current-column)) fill-column)
  1393.       (progn
  1394.     (move-to-column fill-column)
  1395.     (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
  1396.         (delete-horizontal-space)
  1397.       (f90-find-breakpoint)
  1398.       (f90-break-line)
  1399.       (end-of-line)))))
  1400.  
  1401. (defun f90-join-lines ()
  1402.   "Join present line with next line, if this line ends with \&."
  1403.   (interactive)
  1404.   (let (pos (oldpos (point)))
  1405.     (end-of-line)
  1406.     (skip-chars-backward " \t")
  1407.     (cond ((= (preceding-char) ?&)
  1408.        (delete-char -1)
  1409.        (setq pos (point))
  1410.        (forward-line 1)
  1411.        (skip-chars-forward " \t")
  1412.        (if (looking-at "\&") (delete-char 1))
  1413.        (delete-region pos (point))
  1414.        (if (not (f90-in-string))
  1415.            (progn (delete-horizontal-space) (insert " ")))
  1416.        (if (and auto-fill-function
  1417.             (> (save-excursion (end-of-line)
  1418.                        (current-column))
  1419.                fill-column))
  1420.            (f90-do-auto-fill))
  1421.        (goto-char oldpos)
  1422.        t))))
  1423.  
  1424. (defun f90-fill-region (beg-region end-region)
  1425.   "Fill every line in region by forward parsing. Join lines if possible."
  1426.   (interactive "*r")
  1427.   (let ((end-region-mark (make-marker))
  1428.     (f90-smart-end nil) (f90-auto-keyword-case nil) indent (go-on t)
  1429.     (af-function auto-fill-function) (auto-fill-function nil))
  1430.     (set-marker end-region-mark end-region)
  1431.     (goto-char beg-region)
  1432.     (while go-on
  1433.       ;; join as much as possible
  1434.       (while (f90-join-lines));
  1435.       (setq indent (+ (f90-current-indentation) f90-continuation-indent))
  1436.       ;; chop the line if necessary
  1437.       (while (> (save-excursion (end-of-line) (current-column))
  1438.         fill-column)
  1439.     (move-to-column fill-column)
  1440.     (if (and (looking-at "[ \t]*$") (not (f90-in-string)))
  1441.         (delete-horizontal-space)
  1442.       (f90-find-breakpoint)
  1443.       (f90-break-line 'no-update)
  1444.       (f90-indent-to indent 'no-line-no)))
  1445.       (setq go-on (and  (< (point) (marker-position end-region-mark))
  1446.             (zerop (forward-line 1))))
  1447.       (setq f90-cache-position (point)))
  1448.     (setq auto-fill-function af-function)
  1449.     (setq f90-cache-position nil)
  1450.     (if (string-match "XEmacs" emacs-version)
  1451.     (zmacs-deactivate-region)
  1452.       (deactivate-mark))))
  1453.  
  1454. (defun f90-block-match (beg-block beg-name end-block end-name)
  1455.   "Match end-struct with beg-struct and complete end-block if possible.
  1456. Leave point at the end of line."
  1457.   (search-forward "end" (f90-get-end-of-line))
  1458.   (catch 'no-match
  1459.     (if (not (f90-equal-symbols beg-block end-block))
  1460.     (if end-block
  1461.         (progn
  1462.           (message "END %s does not match %s." end-block beg-block)
  1463.           (end-of-line) 
  1464.           (throw 'no-match nil))
  1465.       (message "Inserting %s." beg-block)
  1466.       (insert (concat " " beg-block)))
  1467.       (search-forward end-block))
  1468.     (if (not (f90-equal-symbols beg-name end-name))
  1469.     (cond ((and beg-name (not end-name)) 
  1470.            (message "Inserting %s." beg-name)
  1471.            (insert (concat " " beg-name)))
  1472.           ((and beg-name end-name) 
  1473.            (message "Replacing %s with %s." end-name beg-name)
  1474.            (search-forward end-name)
  1475.            (replace-match beg-name))
  1476.           ((and (not beg-name) end-name) 
  1477.            (message "Deleting %s." end-name)
  1478.            (search-forward end-name)
  1479.            (replace-match "")))
  1480.       (if end-name (search-forward end-name)))
  1481.     (if (not (looking-at "[ \t]*!")) (delete-horizontal-space))))
  1482.  
  1483. (defun f90-match-end ()
  1484.   "From an end foo statement, find the corresponding foo including name."
  1485.   (interactive)
  1486.   (let ((count 1) (top-of-window (window-start)) (matching-beg nil)
  1487.     (end-point (point)) (case-fold-search t)
  1488.     beg-name end-name beg-block end-block end-struct)
  1489.     (if (save-excursion (beginning-of-line) (skip-chars-forward " \t0-9")
  1490.             (setq end-struct (f90-looking-at-program-block-end)))
  1491.     (progn
  1492.       (setq end-block (car end-struct))
  1493.       (setq end-name  (car (cdr end-struct)))
  1494.       (save-excursion
  1495.         (beginning-of-line)
  1496.         (while 
  1497.         (and (not (zerop count))
  1498.              (let ((stop nil) notexist)
  1499.                (while (not stop)
  1500.              (setq notexist
  1501.                    (not (re-search-backward 
  1502.                      (concat "\\(" f90-blocks-re "\\)") nil t)))
  1503.              (if notexist
  1504.                  (setq stop t)
  1505.                (setq stop
  1506.                  (not (or (f90-in-string)
  1507.                       (f90-in-comment))))))
  1508.                (not notexist)))
  1509.           (beginning-of-line) (skip-chars-forward " \t0-9")
  1510.           (cond ((setq matching-beg
  1511.                (cond
  1512.                 ((f90-looking-at-do))
  1513.                 ((f90-looking-at-if-then))
  1514.                 ((f90-looking-at-where-or-forall))
  1515.                 ((f90-looking-at-select-case))
  1516.                 ((f90-looking-at-type-like))
  1517.                 ((f90-looking-at-program-block-start))))
  1518.              (setq count (- count 1)))
  1519.             ((looking-at (concat "end[ \t]*" f90-blocks-re "\\b"))
  1520.              (setq count (+ count 1)))))
  1521.         (if (not (zerop count))
  1522.         (message "No matching beginning.")
  1523.           (f90-update-line)
  1524.           (if (eq f90-smart-end 'blink)
  1525.           (if (< (point) top-of-window)
  1526.               (message "Matches %d: %s"
  1527.                    (what-line)
  1528.                    (buffer-substring
  1529.                 (progn (beginning-of-line) (point))
  1530.                 (progn (end-of-line) (point))))
  1531.             (sit-for 1)))
  1532.           (setq beg-block (car matching-beg))
  1533.           (setq beg-name (car (cdr matching-beg)))
  1534.           (goto-char end-point)
  1535.           (beginning-of-line)
  1536.           (f90-block-match beg-block beg-name end-block end-name)))))))
  1537.  
  1538. (defun f90-insert-end ()
  1539.   "Inserts an complete end statement matching beginning of present block."
  1540.   (interactive)
  1541.   (let ((f90-smart-end (if f90-smart-end f90-smart-end 'blink)))
  1542.     (insert "end")
  1543.     (f90-indent-new-line)))
  1544.  
  1545. ;; abbrevs and keywords
  1546.  
  1547. (defun f90-abbrev-start ()
  1548.   "Typing `\\[help-command] or `? lists all the F90 abbrevs. 
  1549. Any other key combination is executed normally."
  1550.   (interactive)
  1551.   (let (e c)
  1552.     (insert last-command-char)
  1553.     (if (string-match "XEmacs" emacs-version)
  1554.     (progn
  1555.       (setq e (next-command-event))
  1556.       (setq c (event-to-character e)))
  1557.       (setq c (read-event)))
  1558.     ;; insert char if not equal to `?'
  1559.     (if (or (= c ??) (eq c help-char))
  1560.     (f90-abbrev-help)
  1561.       (if (string-match "XEmacs" emacs-version)
  1562.       (setq unread-command-event e)
  1563.     (setq unread-command-events (list c))))))
  1564.  
  1565. (defun f90-abbrev-help ()
  1566.   "List the currently defined abbrevs in F90 mode."
  1567.   (interactive)
  1568.   (message "Listing abbrev table...")
  1569.   (display-buffer (f90-prepare-abbrev-list-buffer))
  1570.   (message "Listing abbrev table...done"))
  1571.  
  1572. (defun f90-prepare-abbrev-list-buffer ()
  1573.   (save-excursion
  1574.     (set-buffer (get-buffer-create "*Abbrevs*"))
  1575.     (erase-buffer)
  1576.     (insert-abbrev-table-description 'f90-mode-abbrev-table t)
  1577.     (goto-char (point-min))
  1578.     (set-buffer-modified-p nil)
  1579.     (edit-abbrevs-mode))
  1580.   (get-buffer-create "*Abbrevs*"))
  1581.  
  1582. (defun f90-upcase-keywords ()
  1583.   "Upcase all F90 keywords in the buffer."
  1584.   (interactive)
  1585.   (f90-change-keywords 'upcase-word))
  1586.  
  1587. (defun f90-capitalize-keywords ()
  1588.   "Capitalize all F90 keywords in the buffer."
  1589.   (interactive)
  1590.   (f90-change-keywords 'capitalize-word))
  1591.  
  1592. (defun f90-downcase-keywords ()
  1593.   "Downcase all F90 keywords in the buffer."
  1594.   (interactive)
  1595.   (f90-change-keywords 'downcase-word))
  1596.  
  1597. (defun f90-upcase-region-keywords (beg end)
  1598.   "Upcase all F90 keywords in the region."
  1599.   (interactive "*r")
  1600.   (f90-change-keywords 'upcase-word beg end))
  1601.  
  1602. (defun f90-capitalize-region-keywords (beg end)
  1603.   "Capitalize all F90 keywords in the region."
  1604.   (interactive "*r")
  1605.   (f90-change-keywords 'capitalize-word beg end))
  1606.  
  1607. (defun f90-downcase-region-keywords (beg end)
  1608.   "Downcase all F90 keywords in the region."
  1609.   (interactive "*r")
  1610.   (f90-change-keywords 'downcase-word beg end))
  1611.  
  1612. ;; Change the keywords according to argument.
  1613. (defun f90-change-keywords (change-word &optional beg end)
  1614.   (save-excursion
  1615.     (setq beg (if beg beg (point-min)))
  1616.     (setq end (if end end (point-max)))
  1617.     (let ((keyword-re 
  1618.        (concat "\\("
  1619.            f90-keywords-re "\\|" f90-procedures-re "\\|"
  1620.            f90-hpf-keywords-re "\\|" f90-operators-re "\\)"))
  1621.       (ref-point (point-min)) state
  1622.       (modified (buffer-modified-p)) saveword back-point)
  1623.       (goto-char beg)
  1624.       (unwind-protect
  1625.       (while (re-search-forward keyword-re end t)
  1626.         (if (progn
  1627.           (setq state (parse-partial-sexp ref-point (point)))
  1628.           (or (nth 3 state) (nth 4 state)
  1629.               (save-excursion    ; Check for cpp directive.
  1630.             (beginning-of-line)
  1631.             (skip-chars-forward " \t0-9")
  1632.             (looking-at "#"))))
  1633.         ()
  1634.           (setq ref-point (point)
  1635.             back-point (save-excursion (backward-word 1) (point)))
  1636.           (setq saveword (buffer-substring back-point ref-point))
  1637.           (funcall change-word -1)
  1638.           (or (string= saveword (buffer-substring back-point ref-point))
  1639.           (setq modified t))))
  1640.     (or modified (set-buffer-modified-p nil))))))
  1641.  
  1642. (provide 'f90)
  1643.  
  1644. ;;; f90.el ends here
  1645.